#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from base.Sequence import Sequence
from base.ChoicesModifier import *
from DV.gpgpu.trees.instruction_tree import (
    ALU_Int_All_instructions,
    ALU_Int32_All_instructions,
)

#  My Operand Choices Modifier class
#  the sub ChoicesModifier class for an example


class MyOperandChoicesModifier(ChoicesModifier):
    def __init__(self, gen_thread):
        super(MyOperandChoicesModifier, self).__init__(gen_thread, "MyOperandChoicesModifier")

    def update(self, **kwargs):
        for arg in kwargs:
            modifies = kwargs[arg]
            for key in modifies:
                self.modifyOperandChoices(key, modifies[key])


class MainSequence(Sequence):
    def generate(self, **kargs):

        my_choices_mod = MyOperandChoicesModifier(self.genThread)
        dict1 = {"GPRs": {"x7": 200, "x15": 200}}
        apply_id1 = my_choices_mod.apply(myarg1=dict1)

        self.notice("Applied choices modifications")
        self.dumpPythonObject(my_choices_mod.getChoicesTreeInfo("GPRs", "OperandChoices"))

        instrs = (
            ALU_Int32_All_instructions
            if self.getGlobalState("AppRegisterWidth") == 32
            else ALU_Int_All_instructions
        )

        for _ in range(50):
            instr = self.pickWeighted(instrs)
            self.genInstruction("ADD##CALM")

        my_choices_mod.revert(apply_id1)

        self.notice("Reverted choices modifications")
        self.dumpPythonObject(my_choices_mod.getChoicesTreeInfo("GPRs", "OperandChoices"))

        for _ in range(50):
            instr = self.pickWeighted(instrs)
            self.genInstruction("ADD##CALM")


#  Points to the MainSequence defined in this file
MainSequenceClass = MainSequence

#  Using GenThreadGPGPU by default, can be overriden with extended classes
GenThreadClass = GenThreadGPGPU

#  Using EnvGPGPU by default, can be overriden with extended classes
EnvClass = EnvGPGPU
